let attachmentLock = false
function AllReady() {
    var mstform = Ext.getCmp('TendAnaly');
    var user_bdmc = mstform.queryById("user_bdmc");
    var user_bspslx = mstform.queryById("user_bspslx");
    // var user_sflhttb = mstform.queryById("user_sflhttb");

    var userID = $appinfo.userID;
    mstform.getItem("PhidPc").userSetMustInput(false)
    mstform.getItem("PhidPc").setVisible(false)

    mstform.getItem("TendResult").userSetMustInput(false)
    mstform.getItem("TendResult").setVisible(false)
    if (!attachmentLock) {
        addWindow_ini()
        document.querySelector("#attachment_close").addEventListener('click', function () {
            window._catch_close()
        })
        const iframe = document.querySelector("#attachment-window")
        const win = iframe.contentWindow
        console.log(`wind`, win);
        // %监消息
        iframe.contentWindow.postMessage(
            { type: 'fromParent', data: 123 },
            '*' // 或者指定具体域名
        );
        window.addEventListener('message', function (e) {
            if (e.data.type === 'fromParent') {
                console.log('收到父页面数据:', e.data.data);
            }
        });
        attachmentLock = true
    }
    if (otype == 'add') {  //新增时
        console.log(otype, userID);
        mstform.getItem('user_tbr').setValue(userID)
        BatchBindCombox([mstform.getItem('user_tbr')])
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份补零
        const day = String(now.getDate()).padStart(2, '0');        // 日期补零
        const currentDate = `${year}-${month}-${day}`;  // 格式：YYYY-MM-DD

        // 获取目标字段（替换"u_your_date_field"为实际字段名）
        const dateField = mstform.getItem('BillDt');

        // 设置字段值
        if (dateField) {
            dateField.setValue(currentDate);
        } else {
            console.warn('日期字段未找到，请检查字段名');
        }

    }

    // mstform.getItem('PhidPctype').userSetMustInput(false);
    user_bdmc.addListener("helpselected", function () {
        let user_bdmc = mstform.getItem('user_bdmc').getValue()
        console.log(user_bdmc);

        execServer(
            "searchNew",
            { user_bdmc: user_bdmc },
            function (res) {
                console.log(res);

                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    console.log(obj);
                    mstform.getItem('BillNo').setValue(obj[0].extendObjects.bill_no)
                    mstform.getItem('PhidPc').setValue(obj[0].extendObjects.phid)
                    // mstform.getItem('user_xmmc').setValue(obj[0].extendObjects.user_xmmc1)
                    BatchBindCombox([mstform.getItem('PhidPc')])
                    mstform.getItem('user_ywlb').setValue(obj[0].extendObjects.user_yelb)
                    BatchBindCombox([mstform.getItem('user_ywlb')])
                    mstform.getItem('user_hylb').setValue(obj[0].extendObjects.user_hylb)
                    BatchBindCombox([mstform.getItem('user_hylb')])
                    mstform.getItem('user_ejhylb').setValue(obj[0].extendObjects.user_ejhylb)
                    BatchBindCombox([mstform.getItem('user_ejhylb')])
                    mstform.getItem('user_sccyllb').setValue(obj[0].extendObjects.user_sccyllb)
                    BatchBindCombox([mstform.getItem('user_sccyllb')])
                    mstform.getItem('user_scfw').setValue(obj[0].extendObjects.user_scfw)
                    BatchBindCombox([mstform.getItem('user_scfw')])
                    mstform.getItem('user_xmms').setValue(obj[0].extendObjects.user_xmms)
                    BatchBindCombox([mstform.getItem('user_xmms')])
                    mstform.getItem('user_tbdw').setValue(obj[0].extendObjects.user_tbdw)
                    BatchBindCombox([mstform.getItem('user_tbdw')])
                    mstform.getItem('user_gzjb').setValue(obj[0].extendObjects.user_gzjb)
                    mstform.getItem('user_yzdw').setValue(obj[0].extendObjects.user_yzdw)
                    mstform.getItem('user_tbsj').setValue(obj[0].extendObjects.user_tbsj)
                    // BatchBindCombox([mstform.getItem('PhidCompany')])
                    // mstform.getItem('user_tbfzr').setValue(obj[0].extendObjects.u_gzfzr)
                    // BatchBindCombox([mstform.getItem('user_tbfzr')])
                    // mstform.getItem('TendDt').setValue(obj[0].extendObjects.user_tbrq)
                    // BatchBindCombox([mstform.getItem('TendDt')])
                    mstform.getItem('user_tbje').setValue(obj[0].extendObjects.user_tzje)

                    mstform.getItem('user_ssjt').setValue(obj[0].extendObjects.user_yzdwssjt)
                    BatchBindCombox([mstform.getItem('user_ssjt')])

                    mstform.getItem('user_s').setValue(obj[0].extendObjects.phid_province)
                    BatchBindCombox([mstform.getItem('user_s')])
                    mstform.getItem('user_cs').setValue(obj[0].extendObjects.phid_city)
                    BatchBindCombox([mstform.getItem('user_cs')])
                    mstform.getItem('user_qx').setValue(obj[0].extendObjects.phid_region)
                    BatchBindCombox([mstform.getItem('user_qx')])
                    // mstform.getItem('user_tbr').setValue(obj[0].extendObjects.user_tbr)
                    // BatchBindCombox([mstform.getItem('user_tbr')])

                }
            }
        );
    });
    mstform.getItem('user_sccyllb').addListener("change", function () {
        let user_sccyllb = mstform.getItem('user_sccyllb').getValue()
        console.log(user_sccyllb);
        if (user_sccyllb == 6870000000000085) {
            mstform.getItem('user_hylb').userSetMustInput(false);
            mstform.getItem('user_hylb').setVisible(false);
        } else {
            mstform.getItem('user_hylb').userSetMustInput(true);
            mstform.getItem('user_hylb').setVisible(true);
        }

    });
    mstform.getItem('user_hylb').addListener("change", function () {
        let user_hylb = mstform.getItem('user_hylb').getValue()
        console.log(user_hylb);
        if (user_hylb == 6870000000000089) {
            mstform.getItem('user_ejhylb').userSetMustInput(true);
            mstform.getItem('user_ejhylb').setVisible(true);
        } else {
            mstform.getItem('user_ejhylb').userSetMustInput(false);
            mstform.getItem('user_ejhylb').setVisible(false);
        }

    });
    // 打开附件
    function openBox() {
        document.querySelector('#box').style.display = 'flex';
    }
    function closeBox() {
        document.querySelector('#box').style.display = 'none';
    }

    window._catch_open = openBox
    window._catch_close = closeBox
}
// 添加新页面弹窗
function receiveFromIframe(data) {
    window._catch_close()
    
}
function addWindow_ini() {
    console.log(`创建`,);
    const url = 'http://10.52.119.173:30847/react/index.html?v=1755246395296#/customform/detail?otype=add&busType=tbqkzj&isSso=0&AppTitle=%E6%8A%95%E6%A0%87%E6%83%85%E5%86%B5%E6%80%BB%E7%BB%93-%E6%96%B0%E5%A2%9E&schemeId=6870000000000258&orgId=1&orgName=%E4%B8%AD%E5%9B%BD%E6%B0%B4%E5%88%A9%E6%B0%B4%E7%94%B5%E7%AC%AC%E5%85%AD%E5%B7%A5%E7%A8%8B%E5%B1%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&menucode=a6120a63-4695-ede0-7ef9-72cefa4ab586'
    const html = `<div id="box" style="display:flex;justify-content:center;align-items:center;background:#00000028;"><div id="box123" style="background:#fff;position: relative;z-index:99;padding: 20px;border: 2px solid #1890ff;border-radius: 12px;box-shadow: 0 0 10px rgba(24,144,255,0.4);animation: borderPulse 2s infinite alternate;display:inline-block;"><button style="position:absolute;top:5px;right:5px;border:none;background:transparent;font-size:16px;cursor:pointer;" id="attachment_close">×</button><iframe id="attachment-window" style="width:60vw;border:0;border-radius:1vw;height:50vh" src="${url}"></iframe></div><div style="@keyframes borderPulse { from { border-color: #1890ff; box-shadow: 0 0 10px rgba(24,144,255,0.4);} to { border-color: #5cc3ff; box-shadow: 0 0 20px rgba(92,195,255,0.7); } }"></div></div>`
    document.querySelector("body").insertAdjacentHTML('beforebegin', html)
}




